<?php

namespace app\index\controller;

use think\Request;
use app\index\BaseController;
use app\common\model\Question;
use app\common\model\CommentMessage;
use app\common\model\payload\Comment as CommentPayload;
use app\common\services\PrivateMessage as PrivateMessageService;

class MessagesComment extends BaseController
{
    /**
     * 分页获取某题目留言
     *
     * @return \think\Response
     * @args qid 题目ID
     * @args page? 页码
     */
    public function fetchQuestionCommentMessages($qid)
    {
        if (!(Question::where('id', $qid)->find())) {
            error('题目不存在');
        }

        return json(
            CommentMessage::with(['commenter'])->where('question_id', $qid)->paginate(15)
        );
    }

    /**
     * 给题目留言
     *
     * @param  \think\Request  $request
     * @return \think\Response
     * @args qid 题目ID
     * @args request_check+ 是否请求重审
     * @args content 留言内容
     */
    public function createQuestionCommentMessage($qid, PrivateMessageService $service)
    {
        $this->validateOrError();

        $request_check = input('request_check');
        $content = input('content');

        /** @var Question $question */
        $question = Question::where('id', $qid)->find();
        if (!$question) {
            error('题目不存在');
        }

        $user = getCurrentUser();

        $message = new CommentMessage();
        $message->question_id = $qid;
        $message->commenter_id = $user->id;
        $message->request_check = $request_check;
        $message->is_rechecked = false;
        $message->content = $content;
        $message->save();

        // 发送私信题目作者
        $paylod = new CommentPayload($question->id);
        $checkerName = $user->realname;
        $content = "{$checkerName}给你的题目留言了：" . $content . "，点击下面的链接查看该题目。";
        $service->with($paylod)->send($content)->to($question->author);

        return json($message);
    }

    /**
     * 修改留言
     *
     * @param  int  $id
     * @return \think\Response
     * @args qid 题目ID
     * @args id 留言ID
     * @args content 留言内容
     */
    public function editQuestionCommentMessage($qid, $id)
    {
        $this->validateOrError();

        $content = input('content');

        /** @var Question $question */
        $question = Question::where('id', $qid)->find();
        if (!$question) {
            error('题目不存在');
        }

        /** @var CommentMessage $message */
        $message = CommentMessage::where('id', $id)->find();
        if (!$message) {
            error('留言不存在');
        }
        if ($message->question_id !== $question->id) {
            error('服务器内部错误，留言数据一致性错误');
        }

        $user = getCurrentUser();
        if ($message->commenter_id !== $user->id) {
            error('您不是这条留言的作者，不能编辑留言');
        }

        $message->content = $content;
        $message->save();

        return json($message);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function removeQuestionCommentMessage($qid, $id)
    {
        /** @var Question $question */
        $question = Question::where('id', $qid)->find();
        if (!$question) {
            error('题目不存在');
        }

        /** @var CommentMessage $message */
        $message = CommentMessage::where('id', $id)->find();
        if (!$message) {
            error('留言不存在');
        }
        if ($message->question_id !== $question->id) {
            error('服务器内部错误，留言数据一致性错误');
        }

        $user = getCurrentUser();
        if ($message->commenter_id !== $user->id) {
            error('您不是这条留言的作者，不能删除留言');
        }

        $message->delete();

        return json([
            'message' => '删除成功'
        ]);
    }

    /**
     * 获取所有需要重审的题目留言
     *
     * @return \think\Response
     * @args is_rechecked+ 是否已经审核过，默认不限
     * @args page? 页码
     */
    public function fetchRecheckCommentMessages(Request $request)
    {
        $query = CommentMessage::with(['commenter'])
            ->where('request_check', 1);
        if ($request->has('is_rechecked')) {
            $isRechecked = boolval($request->is_rechecked) ? 1 : 0;
            $query = $query->where('is_rechecked', $isRechecked);
        } else {
            $query = $query->order('is_rechecked', 'asc');
        }
        return json(
            $query->order('update_at', 'desc')->paginate(15)
        );
    }

    /**
     * 修改是否已经重审标记
     *
     * @return \think\Response
     * @args id 留言ID
     * @args is_rechecked 已复审？
     */
    public function editRecheckCommentMessage($id)
    {
        /** @var CommentMessage $message */
        $message = CommentMessage::where('id', $id)->find();
        if (!$message) {
            error('留言不存在');
        }

        $message->is_rechecked = boolval(input('is_rechecked'));
        $message->save();

        return json([
            'message' => '操作成功'
        ]);
    }
}
